package com.example.springcloud.controller;

import com.example.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-02-06
 * Time: 21:58
 */

@RestController
@Slf4j
@RequestMapping("/consumer")
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")// 全局的兜底方法，避免代码重复
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;



    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        return paymentHystrixService.paymentInfo_OK(id);
    }

    @GetMapping("/payment/hystrix/timeout/{id}")
    // 指定方法
//    @HystrixCommand(fallbackMethod = "paymentInfo_TimeoutFallbackMethod", commandProperties = {
//            // 超时服务降级
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
//    })
    @HystrixCommand // 默认使用全局的方法
    public String paymentInfo_Timeout(@PathVariable("id") Integer id) {
        // 异常也会被兜底
//        int a = 1 / 0;
        return paymentHystrixService.paymentInfo_Timeout(id);
    }

    public String payment_Global_FallbackMethod() {
        return "Global fallback method：最后的防线";
    }


    public String paymentInfo_TimeoutFallbackMethod(@PathVariable("id") Integer id) {
        return "对方系统繁忙或系统出现异常，请等待或重试，id：" + id;
    }
}
